package tuiLayer;
import controlLayer.DvdCtr;
import modelLayer.DVD;
import java.util.Scanner;

/**
 * Write a description of class DVDsMenuUI here.
 * 
 * @author (Yo momma) 
 * @version (a version number or a date)
 */
public class DVDsMenuUI
{

    private DvdCtr ctrl;
    private Scanner scanner;    
    /**
     * Constructor for objects of class DVDsMenuUI
     */
    public DVDsMenuUI()
    {
        ctrl = new DvdCtr();
        scanner = new Scanner(System.in);
    }

    public void start()
    {
        System.out.println("This is the DVDs menu:");
        System.out.println("1. Add DVD");
        System.out.println("2. Update DVD");
        System.out.println("3. Remove DVD");
        System.out.println("4. Add Copy");
        System.out.println("5. Back");
        processInput();
    }
    
    private void processInput()
    {
        
        int input = Integer.parseInt(scanner.nextLine());
       switch(input)
       {
           case 1:
                addDVD();
           break;
           case 2:
                updateDVD();
           break;
           case 3:
                deleteDVD();
           break;
           case 4:
                addCopyToDVD();
           break;
           case 5:
           break;
           default:
            System.out.println("invalid input");
            processInput();
           break;
        }
    }
    
    private void addDVD()
    {
        String barcode, artist, title, publishDate;
        
        barcode = getValue("barcode");
        artist = getValue("artist");
        title = getValue("title");
        publishDate = getValue("publishDate");
        
        ctrl.createDVD(barcode, title, artist, publishDate);
        addCopyToDVD(title);
        start();
    }
    
    private void updateDVD()
    {
        String barcode, artist, title, publishDate;
        String dvdTitle = getValue("title");
        
        DVD dvd = ctrl.getDVD(dvdTitle);
        
        System.out.println("Update info for " + dvd.getTitle());
        
        if(shouldUpdate("barcode"))
            barcode = getValue("barcode");
        else
            barcode = dvd.getBarcode();
            
        if(shouldUpdate("artist"))
            artist = getValue("artist");
        else
            artist = dvd.getArtist();
            
        if(shouldUpdate("title"))
            title = getValue("title");
        else
            title = dvd.getTitle();
            
        if(shouldUpdate("publishDate"))
            publishDate = getValue("publishDate");
        else
            publishDate = dvd.getPublishDate();
            
        ctrl.updateDVD(dvd, barcode, artist, title, publishDate);
        System.out.println("success");
        start();
    }
    
    private void deleteDVD()
    {
        String title = getValue("title");
        
        DVD dvd = ctrl.getDVD(title);
        
        ctrl.deleteDVD(dvd);
        start();
    }
    
     private void addCopyToDVD()
    {
        String title = getValue("DVD title");
        
        addCopyToDVD(title);
    }
    
    private void addCopyToDVD(String title)
    {
        String serialNumber, purchaseDate;
        double purchasePrice;
        
        DVD dvd = ctrl.getDVD(title);
        
        if(dvd == null)
        {
            System.out.println("could not find that DVD");
            start();
        }
        System.out.println("Add new copy:");
        serialNumber = getValue("serialNumber");
        purchaseDate = getValue("purchaseDate");

        purchasePrice = Double.parseDouble(getValue("Price"));
       
        ctrl.addCopyToDVD(serialNumber, purchaseDate, purchasePrice, dvd);
        start();
    }
    
    private boolean shouldUpdate(String name)
    {
        System.out.println("Update " + name + "? Y/N");
        
        String input = scanner.nextLine();
        
        switch(input.toLowerCase())
        {
            case "y":
            return true;
            case "n":
            return false;
            default:
                System.out.println("Invalid input");
                return shouldUpdate(name);
        }
    }
    
    private String getValue(String name)
    {
        System.out.println("Specify " + name + " :");
        return scanner.nextLine();
    }
}
